package com.jtjt.parking.springactivitidemo.controller;

import org.activiti.api.process.model.ProcessDefinition;
import org.activiti.api.process.model.ProcessInstance;
import org.activiti.api.process.model.builders.ProcessPayloadBuilder;
import org.activiti.api.process.runtime.ProcessRuntime;
import org.activiti.api.runtime.shared.query.Page;
import org.activiti.api.runtime.shared.query.Pageable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by hwj on 2019/6/14.
 */
@RestController
@RequestMapping("/processes")
public class ProcessController {

    @Autowired
    ProcessRuntime processRuntime;

    @GetMapping("/processes-def-list")
    public void processDefList() {
        Page<ProcessDefinition> processDefinitionPage = processRuntime.processDefinitions(Pageable.of(0, 10));
        System.out.println("> Available Process definitions: " + processDefinitionPage.getTotalItems());
        for (ProcessDefinition pd : processDefinitionPage.getContent()) {
            System.out.println("\t > Process definition: " + pd);
        }
}

    @GetMapping("/start")
    public void start() {

        ProcessInstance processInstance = processRuntime.start(ProcessPayloadBuilder
                .start()
                .withProcessDefinitionKey("sales-process")
                .withName("sales-process")
                .build());

        System.out.println(">>> Created Process Instance: " + processInstance);
    }

}
